import 'package:async/async.dart';
import 'package:demo1/model/details.dart';
import 'package:demo1/provider/details_info.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
import './details_top_area.dart';
import './details_page/details_explain.dart';
import './details_page/details_web.dart';
import './details_page/details_bottom.dart';

class DetailsPage extends StatefulWidget {
  final String goodsId;

  const DetailsPage({Key key, this.goodsId}) : super(key: key);
  @override
  _DetailsPageState createState() => _DetailsPageState(goodsId);
}

class _DetailsPageState extends State<DetailsPage> {
  final String goodsId;
  final AsyncMemoizer _memoizer = AsyncMemoizer();
  _DetailsPageState(this.goodsId);

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            leading: IconButton(
              icon: Icon(Icons.arrow_back),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
            title: Text('商品详情')),
        body: FutureBuilder(
            future: this._getDetailsInfo(context),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Stack(
                  children: <Widget>[
                    ListView(
                      children: <Widget>[
                        DetailsTopArea(),
                        DetailsExplain(),
                        DetailsWeb()
                      ],
                    ),
                    Positioned(bottom: 0, left: 0, child: DetailsBottom())
                  ],
                );
              } else {
                return Text('加载中……');
              }
            }));
  }

  Future _getDetailsInfo(BuildContext context) async {
    return await this._memoizer.runOnce(() async {
      return await Provider.of<DetailsInfoProvide>(context)
          .getGoodsInfo(goodsId);
    });
  }
}
